package org.ytor.common.util.json.status;

import org.ytor.common.util.json.AbsJsonState;
import org.ytor.common.util.json.JsonFsm;

/**
 * created by yangtong on 2025/4/14 22:41:56
 * <br/>
 * 表示正在读取数值类型的value
 */
public class InNumberValueState extends AbsJsonState {
    @Override
    public void handleNonWhitespace(JsonFsm fsm, char c) {
        //读取数值value时，遇到','或者'}'或者']'，表示读取完毕
        if (c == ',' || c == '}' || c == ']') {
            Object value;
            String numStr = fsm.getBuffer();
            if (numStr.contains(".") || numStr.contains("e") || numStr.contains("E")) {
                value = Double.parseDouble(numStr);
            } else {
                try {
                    value = Integer.parseInt(numStr);
                } catch (NumberFormatException e) {
                    value = Long.parseLong(numStr); // 支持长整型
                }
            }
            fsm.acceptValue(value);

            ExpectCommaOrEndState.handleCommaOrEndState(fsm, c);
        }
        else {
            fsm.writeBuffer();
        }
    }
}
